package com.zhhh.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class RolePowerTree {

	private Integer id;
	private String name;
	private String code;
	private Integer nodeCode;
	private Integer pid;
	private String type;
	private String ctrlCode;
	private String roleCode;
	private String modeCode;
	//默认没有权限
	private Integer ifOper = -1;
	private Integer ifEnable = -1;
	private Integer ifVisible = -1;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String integer) {
		this.code = integer;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public Integer getNodeCode() {
		return nodeCode;
	}

	public void setNodeCode(Integer nodeCode) {
		this.nodeCode = nodeCode;
	}
	
	
	public Integer getIfOper() {
		return ifOper;
	}

	public void setIfOper(Integer ifOper) {
		this.ifOper = ifOper;
	}

	public Integer getIfEnable() {
		return ifEnable;
	}

	public void setIfEnable(Integer ifEnable) {
		this.ifEnable = ifEnable;
	}

	public Integer getIfVisible() {
		return ifVisible;
	}

	public void setIfVisible(Integer ifVisible) {
		this.ifVisible = ifVisible;
	}
	
	
	

	public static List<BRole> getTreeJson(List<BRole> roleList) {

		for (int i = 0; i < roleList.size(); i++) {
			List<RolePowerTree> treeJsonList = new ArrayList<RolePowerTree>();
			int nodeCode = 1000;
			BRole role = roleList.get(i);
			Set<BMode> modes = role.getModes();
			if (modes.size() > 0) {
				for (BMode mode : modes) {
					RolePowerTree treeNode = new RolePowerTree();
					treeNode.setId(nodeCode);
					treeNode.setCode(mode.getModeCode());
					treeNode.setName(mode.getModeName());
					treeNode.setType("1");
					treeNode.setNodeCode(mode.getId());
					treeNode.setPid(0);// 模块是顶级 没有父节点
					treeJsonList.add(treeNode);
					Set<BModeControl> modeControls = mode.getModeControls();
					if (modeControls.size() > 0) {
						int modeControlCount = 1;
						for (BModeControl modeControl : modeControls) {
							RolePowerTree treeNode1 = new RolePowerTree();
							treeNode1.setId(nodeCode * 1000 + modeControlCount);
							treeNode1.setCode(modeControl.getCtrlId());
							treeNode1.setName(modeControl.getCtrlText());
							treeNode1.setNodeCode(modeControl.getId());
							treeNode1.setType("2");
							treeNode1.setCtrlCode(modeControl.getCtrlId());
							treeNode1.setModeCode(mode.getModeCode());
							treeNode1.setRoleCode(role.getRoleCode());
							treeNode1.setPid(treeNode.getId());// 模块是顶级 没有父节点
							treeJsonList.add(treeNode1);

							Set<BModeOper> modeOpers = modeControl.getModeOpers();
							//Set<BModeEm> modeEms = modeControl.getModeEms();
							if (modeOpers.size() > 0) {
								int modeOperCount = 1;
								for (BModeOper modeOper : modeOpers) {
									RolePowerTree treeNode2 = new RolePowerTree();
									treeNode2.setId(treeNode1.getId() * 1000 + modeOperCount);
									treeNode2.setCode(modeOper.getOperId());
									treeNode2.setName(modeOper.getOperText());
									treeNode2.setNodeCode(modeOper.getId());
									treeNode2.setType("3");
									treeNode2.setPid(treeNode1.getId());
									
									treeNode2.setCtrlCode(modeControl.getCtrlId());
									treeNode2.setRoleCode(role.getRoleCode());

									/**
									 *  循环检查该角色是否有权限操作
									 */
									for (BRoleModeOper roleOper : role.getRoleOpers()) {
										if (modeOper.getOperId().equals(roleOper.getOperId())
												&& role.getRoleCode().equals(roleOper.getRoleCode())) {
											treeNode2.setIfOper(roleOper.getIfOper());
										}
									}
									
									treeJsonList.add(treeNode2);
									modeOperCount++;
								}
							}
							modeControlCount++;
						}

					}
					nodeCode++;
				}
			}
			roleList.get(i).setChildList(treeJsonList);
		}
		return roleList;
	}

	public static List<RolePowerTree> getTreeJson(BRole role) {
		List<RolePowerTree> treeJsonList = new ArrayList<RolePowerTree>();
		int nodeCode = 1000;
		Set<BMode> modes = role.getModes();
		if (modes.size() > 0) {
			for (BMode mode : modes) {
				RolePowerTree treeNode = new RolePowerTree();
				treeNode.setId(nodeCode);
				treeNode.setCode(mode.getModeCode());
				treeNode.setName(mode.getModeName());
				treeNode.setType("1");
				treeNode.setNodeCode(mode.getId());
				treeNode.setPid(0);// 模块是顶级 没有父节点
				treeJsonList.add(treeNode);
				Set<BModeControl> modeControls = mode.getModeControls();
				if (modeControls.size() > 0) {
					int modeControlCount = 1;
					for (BModeControl modeControl : modeControls) {
						RolePowerTree treeNode1 = new RolePowerTree();
						treeNode1.setId(nodeCode * 1000 + modeControlCount);
						treeNode1.setCode(modeControl.getCtrlId());
						treeNode1.setName(modeControl.getCtrlText());
						treeNode1.setNodeCode(modeControl.getId());
						treeNode1.setType("2");
						treeNode1.setCtrlCode(modeControl.getCtrlId());
						treeNode1.setModeCode(mode.getModeCode());
						treeNode1.setPid(treeNode.getId());// 模块是顶级 没有父节点
						treeNode1.setRoleCode(role.getRoleCode());
						treeJsonList.add(treeNode1);

						Set<BModeOper> modeOpers = modeControl.getModeOpers();
						// Set<BModeEm> modeEms = modeControl.getModeEms();
						if (modeOpers.size() > 0) {
							int modeOperCount = 1;
							for (BModeOper modeOper : modeOpers) {
								RolePowerTree treeNode2 = new RolePowerTree();
								treeNode2.setId(treeNode1.getId() * 1000 + modeOperCount);
								treeNode2.setCode(modeOper.getOperId());
								treeNode2.setName(modeOper.getOperText());
								treeNode2.setNodeCode(modeOper.getId());
								treeNode2.setType("3");
								treeNode2.setPid(treeNode1.getId());
								treeNode2.setCtrlCode(modeControl.getCtrlId());
								treeNode2.setRoleCode(role.getRoleCode());
								treeNode2.setModeCode(mode.getModeCode());

								/**
								 *  循环检查该角色是否有权限操作
								 */
								for (BRoleModeOper roleOper : role.getRoleOpers()) {
									if (modeOper.getOperId().equals(roleOper.getOperId())
											&& role.getRoleCode().equals(roleOper.getRoleCode())) {
										treeNode2.setIfOper(roleOper.getIfOper());
									}
								}

								treeJsonList.add(treeNode2);
								modeOperCount++;
							}
						}
//						if (modeEms.size() > 0) {
//							int modeEmCount = 1;
//							for (BModeEm modeEm : modeEms) {
//								RolePowerTree treeNode3 = new RolePowerTree();
//								treeNode3.setId(treeNode1.getId() * 5000 + modeEmCount);
//								treeNode3.setCode(modeEm.getEmId());
//								treeNode3.setName(modeEm.getEmText());
//								treeNode3.setNodeCode(modeEm.getId());
//								treeNode3.setType("4");
//								treeNode3.setPid(treeNode1.getId());
//								treeJsonList.add(treeNode3);
//								modeEmCount++;
//							}
//						}
						modeControlCount++;
					}
				}
				nodeCode++;
			}
		}
		return treeJsonList;
	}

	public String getCtrlCode() {
		return ctrlCode;
	}

	public void setCtrlCode(String ctrlCode) {
		this.ctrlCode = ctrlCode;
	}

	public String getRoleCode() {
		return roleCode;
	}

	public void setRoleCode(String roleCode) {
		this.roleCode = roleCode;
	}

	public String getModeCode() {
		return modeCode;
	}

	public void setModeCode(String modeCode) {
		this.modeCode = modeCode;
	}

	
}
